﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lm.Common.ExtensionMethods
{
    public static class StringListExt
    {
        /// <summary>
        /// 把列表拼成字符串
        /// </summary>
        /// <param name="list"></param>
        /// <param name="separator">字符串的间隔符</param>
        /// <returns></returns>
        public static string JoinString(this IList<string> list, string separator)
        { return JoinString(list, separator, "", ""); }

        /// <summary>
        /// 把列表拼成字符串
        /// </summary>
        /// <param name="list"></param>
        /// <param name="separator">字符串的间隔符</param>
        /// <param name="mark">每个字符的边界，如1,2-->'1','2',其中“'”用此参数指定</param>
        /// <returns></returns>
        public static string JoinString(this IList<string> list, string separator, string mark)
        { return JoinString(list, separator, mark, mark); }

        /// <summary>
        /// 把列表拼成字符串，注意：边界，如1,2-->'1','2',其中“'”用此参数指定
        /// </summary>
        /// <param name="list"></param>
        /// <param name="separator">字符串的间隔符</param>
        /// <param name="itemStart">前边界</param>
        /// <param name="itemEnd">后边界</param>
        /// <returns></returns>
        public static string JoinString(this IList<string> list, string separator, string itemStart, string itemEnd)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                sb.Append(itemStart);
                sb.Append(list[i]);
                sb.Append(itemEnd);
                if (i != list.Count - 1)
                { sb.Append(separator); }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 比较两个列表是否相同
        /// </summary>
        /// <param name="list1"></param>
        /// <param name="list2"></param>
        /// <returns></returns>
        public static bool SameWith(this List<string> list1, List<string> list2)
        {
            if (list1.Count != list2.Count)
            { return false; }
            var c1 = list1.Copy();
            var c2 = list1.Copy();
            c1.Sort();
            c2.Sort();
            for (int i = 0; i < c1.Count; i++)
            {
                if (c1[i] != c2[i])
                { return false; }
            }
            return true;
        }

        /// <summary>
        /// 复制一份列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<string> Copy(this List<string> list)
        {
            string[] arr = new string[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                arr[i] = list[i];
            }
            return arr.ToList();
        }
    }
}
